package com.bl.product.service.impl;

import com.bl.basic.exception.BusinessException;
import com.bl.basic.service.IBaiduAiAuditService;
import com.bl.basic.util.LoginContext;
import com.bl.org.domain.Employee;
import com.bl.org.mapper.EmployeeMapper;
import com.bl.pet.domain.Pet;
import com.bl.pet.domain.PetOnlineAuditLog;
import com.bl.product.domain.Product;
import com.bl.product.domain.ProductDetail;
import com.bl.product.mapper.ProductDetailMapper;
import com.bl.product.mapper.ProductMapper;
import com.bl.product.service.IProductService;
import com.bl.basic.service.impl.BaseServiceImpl;
import com.bl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapperMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    /**
     *  修改重构
     */
    @Transactional
    @Override
    public void update(Product product) {
        //1.上架之前对文本和图片做审核
        Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
        Boolean imageBoo = baiduAiAuditService.imageAudit(product.getResources());

        if (textBoo && imageBoo) {
                //2.审核通过：
                super.update(product);
                ProductDetail productDetail = product.getProductDetail();
                if (product != null){
                    productDetail.setProductId(product.getId());
                    productDetailMapperMapper.updateDetailByproductId(productDetail);
                }
            }else {
                // 失败 返回移长信息
                throw new BusinessException("数据异常");
            }

    }

    /**
     *  添加重构
     */
    @Override
    public void save(Product product) {
        //1.上架之前对文本和图片做审核
        Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
        Boolean imageBoo = baiduAiAuditService.imageAudit(product.getResources());
        if (textBoo && imageBoo) {
            //2.审核通过：修改状态 + 添加上架的审核成功日志
            super.save(product); // 添加product 获取自增id
            ProductDetail productDetail = product.getProductDetail();
            if (productDetail != null){
                productDetail.setProductId(product.getId());
                // 根据自增id添加详情
                productDetailMapperMapper.save(productDetail);
            }
        }else {
            // 失败 返回移长信息
            throw new BusinessException("数据异常");
        }


    }
    /**
     * 删除重构
     */
    @Override
    public void delete(Long id) {
        super.delete(id);

        productDetailMapperMapper.deleteProductDetailByProductId(id);
    }


    /**
     *  批量删除重构
     */
    @Override
    public void patchDelete(Long[] ids) {
        super.patchDelete(ids);

        productDetailMapperMapper.patchDeleteProductDetailByProductId(ids);

    }

    /**
     *  服务上架
     * @param ids
     * @param request
     */
    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        for (Long id : ids) {
             Product product  = productMapper.loadById(id);
            //1.上架之前对文本和图片做审核
            Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
            Boolean imageBoo = baiduAiAuditService.imageAudit(product.getResources());
            if (textBoo && imageBoo) {
                //2.审核通过：修改状态 + 修改审核时间
                product.setOnsaletime(new Date());
                product.setState(1L);
                productMapper.update(product);
            }
        }
    }

    /**
     *  服务下架
     * @param ids
     * @param request
     */
    @Override
    public void offSale(Long[] ids, HttpServletRequest request) {
        for (Long id : ids) {
            Product product  = productMapper.loadById(id);
            product.setOffsaletime(new Date());
            product.setState(0L);
            productMapper.update(product);
        }
    }
}
